package com.weighttracker.android;

import java.text.NumberFormat;

import com.bappi.custom.activities.CustomActivity;
import com.bappi.utils.Utils;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Tab3Activity24 extends CustomActivity{
	
	private EditText weightField,heightFeetsField,heightInchesField,resultFiled;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.layout_tab_3_screen_24);
			weightField = (EditText)findViewById(R.id.weight_field);
			heightFeetsField = (EditText)findViewById(R.id.height_feets_field);
			heightInchesField = (EditText)findViewById(R.id.height_inches_field);
			resultFiled = (EditText)findViewById(R.id.result_field);
			findViewById(R.id.button_back).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					finish();
				}
			});
			findViewById(R.id.button_clear).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					weightField.setText("");
					heightFeetsField.setText("");
					heightInchesField.setText("");
					resultFiled.setText("");
				}
			});
			findViewById(R.id.button_calculate).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					double weight  = 0.0;
					int feets = 0;
					int inches = 0;
					String text;
					try {
						text = weightField.getText().toString();
						if(text.length()>0){
							weight = Double.parseDouble(text);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					try {
						text = heightFeetsField.getText().toString();
						if(text.length()>0){
							feets = Integer.parseInt(text);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					try {
						text = heightInchesField.getText().toString();
						if(text.length()>0){
							inches = Integer.parseInt(text);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					int height = (feets*12)+inches;
					if(weight<=0 || height<=0){
						Utils.showAlertMessage(getParent(), getString(R.string.message_fill_all_required_fiels));
					}else{
						double value =  (weight*703)/Math.pow(height, 2);
						NumberFormat nf = NumberFormat.getInstance();
						nf.setMaximumFractionDigits(2);
						nf.setMinimumFractionDigits(2);
						resultFiled.setText(nf.format(value));

					}
				}
			});
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
}
